package ink.ykb.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.entity.DealerDepartment;
import ink.ykb.demo.permission.entity.Rule;
import ink.ykb.demo.permission.entity.UserDealerDepartment;
import ink.ykb.demo.user.service.UserService;


@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private MessageUtil messageUtil;
	@Autowired
	private UserService userService;

	/**
	 * 获取用户菜单树
	 * @return
	 */
	@GetMapping("/menu")
	public ResponseMsg<List<Rule>> getUserMenu() {
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
				userService.getUserMenu());
	}
	
	/**
	 * 当前用户创建子部门
	 * @param dealerDepartment
	 */
	@PostMapping("/dealerDepartment/add")
	public ResponseMsg<String> addDealerDepartment(DealerDepartment dealerDepartment){
		userService.addDealerDepartment(dealerDepartment);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 当前用户修改子部门
	 * @param dealerDepartment
	 */
	@PostMapping("/dealerDepartment/update")
	public ResponseMsg<String> updateDealerDepartment(DealerDepartment dealerDepartment){
		userService.updateDealerDepartment(dealerDepartment);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 当前用户递归删除子部门
	 * @param dealerDepartmentId
	 */
	@PostMapping("/dealerDepartment/detele")
	public ResponseMsg<String> deleteDealerDepartment(Long dealerDepartmentId){
		userService.deleteDealerDepartment(dealerDepartmentId);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 当前用户添加用户到指定部门
	 * @param userId
	 * @param dealerDepartmentId
	 * @param scope
	 */
	@PostMapping("/dealerDepartment/user/add")
	public ResponseMsg<String> addUserDealerDepartment(
			Long dealerDepartmentId,Long userId,Integer scope){
		userService.addUserDealerDepartment(dealerDepartmentId, userId,scope);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 当前用户修改用户到指定部门
	 * @param userId
	 * @param dealerDepartmentId
	 * @param scope
	 */
	@PostMapping("/dealerDepartment/user/update")
	public ResponseMsg<String> updateUserDealerDepartment(
			Long dealerDepartmentId,Long userId,Integer scope){
		userService.updateUserDealerDepartment(dealerDepartmentId, userId,scope);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 当前用户删除指定部门的用户
	 * @param userId
	 */
	@PostMapping("/dealerDepartment/user/delete")
	public ResponseMsg<String> deleteUserDealerDepartment(Long userId){
		userService.deleteUserDealerDepartment(userId);
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
	}
	
	/**
	 * 获取用户的经销商部门树 <br>
	 * 注意：结构为 根部门 --> 当前用户部门 --> 所有能到达的根部门
	 * @return
	 */
	@GetMapping("/dealerDepartment/tree")
	public ResponseMsg<List<DealerDepartment>> getUserDealerDepartmentTree(){
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
				userService.getUserDealerDepartmentTree());
	}
	
	/**
	 * 获取用户的可查询到的经销商部门的用户权限范围 
	 * @return 用户部门集合
	 */
	@GetMapping("/dealerDepartment/userDealerDepartmentList")
	public ResponseMsg<List<UserDealerDepartment>> getUsersByDealerDepartmentPermission(){
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
				userService.getUsersByDealerDepartmentPermission());
	}
	
	/**
	 * 获取用户可以查看的经销商部门权限范围
	 * @return 经销商部门权限集合
	 */
	@GetMapping("/dealerDepartment/dealerDepartmentList/all")
	public ResponseMsg<List<DealerDepartment>> getUserDealerDepartmentPermission(){
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
				userService.getUserDealerDepartmentPermission());
	}
	
	/**
	 * 根据用户权限和部门ID查询部门下的用户
	 * @param dealerDepartmentId
	 * @return
	 */
	@GetMapping("/dealerDepartment/dealerDepartmentList/get")
	public ResponseMsg<List<UserDealerDepartment>> getUsersByDealerDepartmentPermission(Long dealerDepartmentId){
		return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
				userService.getUsersByDealerDepartmentPermission(dealerDepartmentId));
	}
}
